package io.github.jyrmc.deploy.utils

import com.jcraft.jsch.Channel
import com.jcraft.jsch.ChannelSftp
import com.jcraft.jsch.Session


object SFTPUploader {

    fun uploadFile(
        session: Session,
        localFile: String,
        remoteFile: String
    ): Boolean {
        var channel: Channel? = null
        var channelSftp: ChannelSftp? = null
        return try {
            println("🐙🐙🐙 Start to upload file 🐙🐙🐙")
            channel = session.openChannel("sftp")
            channel.connect()
            channelSftp = channel as ChannelSftp?

            // 上传文件
            channelSftp?.put(localFile, remoteFile)
            println("File uploaded successfully: $remoteFile")

            true
        } catch (e: Exception) {
            e.printStackTrace()
            false
        } finally {
            if (channelSftp != null && channelSftp.isConnected) {
                channelSftp.exit()
            }
            if (channel != null && channel.isConnected) {
                channel.disconnect()
            }
        }
    }
}